﻿using System;
using Projecturo.Model;
using Neits.UserService;
using Projecturo.ProjecturoClient.Connection;
using Projecturo.ProjecturoClient.Command;
using System.Windows.Input;

namespace Projecturo.ProjecturoClient.ViewModel.Project
{
    public class TimeRecordViewModel : WorkspaceViewModel
    {
        private TimeRecord timeRecord;
        private DelegateCommand startCommand;
        private DelegateCommand endCommand;

        public TimeRecordViewModel()
        {
            timeRecord = new TimeRecord();
        }

        public ICommand StartCommand
        {
            get
            {
                if (startCommand == null)
                    startCommand = new DelegateCommand(Start, CanStart);
                return startCommand;
            }
        }

        private void Start()
        {
            StartDate = DateTime.Now;
        }

        public bool CanStart()
        {
            return StartDate == DateTime.MinValue;
        }

        public ICommand EndCommand
        {
            get
            {
                if (endCommand == null)
                    endCommand = new DelegateCommand(End, CanEnd);
                return endCommand;
            }
        }

        private void End()
        {
            EndDate = DateTime.Now;
        }

        public bool CanEnd()
        {
            return EndDate == DateTime.MinValue && StartDate > DateTime.MinValue;
        }

        public override string DisplayName
        {
            get
            {
                return "New Time Record";
            }
            protected set
            {
                base.DisplayName = value;
            }
        }

        public Int64 Id
        {
            get { return timeRecord.Id; }
        }

        public String Name
        {
            get { return timeRecord.Name; }
            set
            {
                if (timeRecord.Name == value)
                    return;
                timeRecord.Name = value;
                OnPropertyChanged("Name");
            }
        }

        public String Description
        {
            get { return timeRecord.Description; }
            set
            {
                if (timeRecord.Description == value)
                    return;
                timeRecord.Description = value;
                OnPropertyChanged("Description");
            }
        }

        public DateTime StartDate
        {
            get { return timeRecord.StartDate; }
            set
            {
                if (timeRecord.StartDate == value)
                    return;
                timeRecord.StartDate = value;
                OnPropertyChanged("StartDate");
            }
        }

        public DateTime EndDate
        {
            get { return timeRecord.EndDate; }
            set
            {
                if (timeRecord.EndDate == value)
                    return;
                timeRecord.EndDate = value;
                OnPropertyChanged("EndDate");
            }
        }

        public IUser Creator
        {
            get { return timeRecord.CreatedBy; }
        }

        public DateTime CreateDate
        {
            get { return timeRecord.CreateDate; }
        }

        protected override void Save()
        {
            base.Save();
            Connector.Connectivity.Storage.Save(timeRecord);

            ViewModelBase parentModel = ViewModelManager.Instance.GetViewModel(ViewModelReference.ParentViewModelId);
            parentModel.Update();
        }

        protected override bool CanSave()
        {
            return StartDate > DateTime.MinValue && EndDate > DateTime.MinValue;
        }

        public override void SetParent(object parent)
        {
            timeRecord.Ticket = parent as Ticket;
        }
    }
}
